package com.settlement.system.controller;

import com.settlement.system.common.enums.WpBillAduitEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpAdjustBillDtl;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpAdjustBillDtlService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * WP-导入调整明细 前端控制器
 * @author xiaochuan
 * @since 2022-07-26
 */

@Api(tags = "WP-导入调整明细",description = "WP-导入调整明细")
@RestController
@RequestMapping("/wp/adjust/bill/dtl")
public class TblWpAdjustBillDtlController extends ExcelController<TblWpAdjustBillDtl> {

    private static final String TUIGONG_BILL_ERROR_MSG = "退供差异对应单号不能为空，请检查数据！";

    private static final String LAIHUO_DIFF_SUM_ERROR_MSG = "来货差异来源调整数量总计不为0,请检查数据！";

    private static final String TUIGONG_DIFF_SUM_ERROR_MSG = "退供差异来源调整数量总计不为0,请检查数据！";

    @Resource
    private ITblWpAdjustBillDtlService tblWpAdjustBillDtlService;

    @Override
    public IBaseService init() {
        return tblWpAdjustBillDtlService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpAdjustBillDtl.class;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "poNo", "barCode", "billNo");
        FasUtil.toUpperCase(model, "poNo","barCode","billNo");
        super.setParamsModel(model);
    }

    @Override
    protected void setFootModel(Map<String, Object> result, Map<String, Object> model) throws ServiceException {
        TblWpAdjustBillDtl foot = tblWpAdjustBillDtlService.getFoot(model);
        result.put("foot", foot==null? getModelClass():foot);
    }

    @Override
    public String verifyImportObject(TblWpAdjustBillDtl tblWpAdjustBillDtl, AnalysisContext analysisContext) {
        String errorMessage = super.verifyImportObject(tblWpAdjustBillDtl, analysisContext);
        if(StringUtils.isNotEmpty(errorMessage)){
            return errorMessage;
        }
        if(StringUtils.equals(tblWpAdjustBillDtl.getClassify(), WpBillAduitEnum.CLASSIFY.TUIGONG_CHAYI.getNo())){
            if(StringUtils.isEmpty(tblWpAdjustBillDtl.getBillNo())){
                return TUIGONG_BILL_ERROR_MSG;
            }
        }
        return null;
    }

    @Override
    protected String validateAllDate(List<TblWpAdjustBillDtl> datas) {
        if(!CollectionUtils.isEmpty(datas)){
            List<TblWpAdjustBillDtl> sumDtlList =
                     datas.stream()
                        .collect(Collectors.groupingBy(TblWpAdjustBillDtl::getClassify))
                        .entrySet()
                        .stream()
                        .map(entry -> {
                            String key = entry.getKey();
                            List<TblWpAdjustBillDtl> value = entry.getValue();
                            int sum = value.stream().mapToInt(TblWpAdjustBillDtl::getAdjustQty).sum();
                            return new TblWpAdjustBillDtl(key, sum);
                        })
                        .collect(Collectors.toList());
            StringBuilder errorMsg = new StringBuilder();
            for(TblWpAdjustBillDtl dtl : sumDtlList){
                if(dtl.getAdjustQty() != 0 && StringUtils.equals(dtl.getClassify(), WpBillAduitEnum.CLASSIFY.LAIHUO_CHAYI.getNo())){
                    errorMsg.append(LAIHUO_DIFF_SUM_ERROR_MSG);
                }
                if(dtl.getAdjustQty() != 0 &&  StringUtils.equals(dtl.getClassify(), WpBillAduitEnum.CLASSIFY.TUIGONG_CHAYI.getNo())){
                    errorMsg.append(TUIGONG_DIFF_SUM_ERROR_MSG);
                }
            }
            return errorMsg.toString();
        }
        return null;
    }
}

